using System;
using System.Data;
using System.Data.SqlClient;

namespace MessageBoard 
{
	/// <summary>
	/// Summary description for CustomControl.
	/// </summary>
	public class CustomControl : Connection
	{
		private int _Id;
		private string _Title;

     	public CustomControl()
		{
			//
			// TODO: Add constructor logic here
			//
		}

		public static CustomControl Get(System.Data.DataRow dr)
		{
			CustomControl customControl = new CustomControl();
			customControl.Id = int.Parse(dr["Id"].ToString());
			customControl.Title = dr["Title"].ToString();
			return customControl;
		}

		public static CustomControl Get(int id)
		{
			DataSet ds = new DataSet();
			SqlDataAdapter getData = new SqlDataAdapter("SELECT * FROM " + Connection.tblCustomControl + " WHERE [Id] = @Id", DbConnection);
			getData.SelectCommand.Parameters.Add("@Id", id);
			getData.Fill(ds);
			if(ds.Tables[0].Rows.Count>0)
			{
				return Get(ds.Tables[0].Rows[0]);
			}
			else
			{
				return null;
			}
		}

		public static CustomControl Get(string title)
		{
			DataSet ds = new DataSet();
			SqlDataAdapter getData = new SqlDataAdapter("SELECT * FROM " + Connection.tblCustomControl + " WHERE [Title] = @Title", DbConnection);
			getData.SelectCommand.Parameters.Add("@Title", title);
			getData.Fill(ds);
			if(ds.Tables[0].Rows.Count>0)
			{
				return Get(ds.Tables[0].Rows[0]);
			}
			else
			{
				return null;
			}
		}

		public static System.Collections.ArrayList GetAll()
		{
			System.Collections.ArrayList arrayList = new System.Collections.ArrayList();
			DataSet ds = new DataSet();
			SqlDataAdapter getData = new SqlDataAdapter("SELECT * FROM " + Connection.tblCustomControl + "", DbConnection);
			getData.Fill(ds);
			foreach(System.Data.DataRow dr in ds.Tables[0].Rows)
			{
				arrayList.Add(Get(dr));
			}
			return arrayList;
		}

		
		public void Insert()
		{
			DataSet ds = new DataSet();
			SqlDataAdapter getData = new SqlDataAdapter(@"
			INSERT INTO " + Connection.tblCustomControl + @" 
			([Title])
			VALUES
			(@Title);
			SELECT @@identity as i", DbConnection);
			getData.SelectCommand.Parameters.Add("@Title", Title);
			getData.Fill(ds);
			if(ds.Tables[0].Rows.Count>0)
			{
				Id = int.Parse(ds.Tables[0].Rows[0]["i"].ToString());
			}
		}

		public void Update()
		{
			DataSet ds = new DataSet();
			SqlDataAdapter getData = new SqlDataAdapter(@"
			UPDATE " + Connection.tblCustomControl + @" SET 
			[Title] = @Title
			WHERE [Id] = @Id", DbConnection);
			getData.SelectCommand.Parameters.Add("@Title", Title);
			getData.SelectCommand.Parameters.Add("@Id", Id);
			getData.Fill(ds);
		}

				
		#region Properties
		/// <summary>
		/// The Id of the thread.
		/// </summary>
		public int Id
		{
			get
			{
				return _Id;
			}
			set
			{
				_Id = value;
			}
		}

		/// <summary>
		/// The Title.
		/// </summary>
		public string Title
		{
			get
			{
				return _Title;
			}
			set
			{
				_Title = value;
			}
		}

		
		#endregion
	}
}
